/**
 * 详情对话框
 */
var StoragePutSingleInfoDlg = {
    data: {
    	positionNumber: "",
    }
};
/**
 * 添加或者修改页面
 */
var OrderPutDetailedInfoDlg = {
    data: {
        detailedGoodsname: "",
        detailedGoodsnum: "",
        goodsNumber: "",
		goodsNo:"",
        detailedRebubbles: "",
        detailedPacking: "",
        detailedLargeclass: "",
        detailedGoodstype: "",
        detailedPutcondition: "",
        detailedPiecenum: "",
        goodsWeight: "",
        goodsVolume: "",
    }
};
layui.use(['table', 'admin', 'ax','laydate','form','element'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var form = layui.form;
	var element = layui.element
	
	//tab切换
	element.on('tab(doTabBrief)',function(data){
		console.log(data.index)
	})
	
    /**
     * 库存表管理
     */
    var StorageStock = {
        tableId: "storageStockTable"
    };

    /**
     * 存货处置记录管理
     */
    var StorageStockManagement = {
        tableId: "storageStockManagementTable"
    };

    /**
     * 初始化表格的列
     */
    StorageStock.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'ID'},
            {field: 'stockstate', title: 'Status',templet: '#stockstateTpl'},
            {field: 'putTime', title: 'Receipt Batch', sort: true},
            {field: 'operateName', title: 'Project'},
            {field: 'customerName', title: 'Customer'},
            {field: 'warehouseName', title: 'Warehouse'},
            {field: 'areaNumber', title: 'Area'},
            {field: 'positionNumber', title: 'Location', style:'background-color: #ecf5ff;font-weight: 600;', sort: true},
            {field: 'trayNumber', title: 'Tray No', style:'background-color: #ecf5ff;font-weight: 600;'},
            {field: 'goodsName', title: 'Product', style:'background-color: #dbf1e1;font-weight: 600;',width:270},
            {field: 'goodsNumber', title: 'Product No', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'goodsNo', title: 'SKU', sort: true, style:'background-color: #dbf1e1;font-weight: 600;', totalRowText: 'Total:'},
            {field: 'num', title: 'Quantity', totalRow: true, style:'background-color: #dbf1e1;font-weight: 600;', sort: true},
            /*{field: 'storageNumber', title: 'Pending Picking Qty', totalRow: true, style:'background-color: #dbf1e1;font-weight: 600;'},*/
            {field: 'producetime', title: 'Production Date', style:'background-color: #dbf1e1;font-weight: 600;', sort: true},
            {field: 'goodsbatchs', title: 'Batch', style:'background-color: #dbf1e1;font-weight: 600;', sort: true},
            {field: 'stockWeight', title: 'Weight', totalRow: true},
            {field: 'stockVolume', title: 'Volume', totalRow: true},
            {field: 'stockMoney', title: 'Amount', totalRow: true},
            {field: 'unitprice', title: 'Unit Price'},
            {field: 'goodsBigtype', title: 'Category'},
            {field: 'goodsType', title: 'Type', hide: true},
            {field: 'goodsUnit', title: 'Unit'},
            {field: 'goodsPacking', title: 'Packing'},
            {field: 'goodsSpecs', title: 'Specs'},
            {field: 'goodsWarranty', title: 'Warranty(days)'},
            {fixed: 'right',align: 'center', toolbar: '#tableBar', title: 'Action',width:180}
        ]];
    };

    /**
     * 点击查询按钮
     */
    StorageStock.search = function () {
        var queryData = {};
        queryData['stockstate'] = $("#stockstate").val();
        queryData['operateId'] = $("#operateId").val();
        queryData['customerId'] = $("#customerId").val();
        queryData['warehouseId'] = $("#warehouseId").val();
        queryData['areaId'] = $("#areaId").val();
        queryData['positionNumber'] = $("#positionNumber").val();
        queryData['goodsNumber'] = $("#goodsNumber").val();
        queryData['goodsName'] = $("#goodsName").val();
        queryData['goodsNo'] = $("#goodsNo").val();
        queryData['trayNumber'] = $("#trayNumber").val();
        table.reload(StorageStock.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 导出excel按钮
     */
    StorageStock.exportExcel = function () {
        var checkRows = table.checkStatus(StorageStock.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("Please select data to export");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + StorageStock.tableId,
        url: Feng.ctxPath + '/storageStock/stockListGoodsChuzhi',
        page: true,
        limit: 60,
		limits:[60,120,240,360,480,1500,5000,10000,20000,80000],
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        totalRow: true ,//开启合计行
        cols: StorageStock.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
    	StorageStock.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
    	StorageStock.exportExcel();
    });

    //点击选择库位
    $('#choicep').click(function () {
        layer.open({
            type: 2,
            title: '选择库位',
            area: ['900px', '500px'],
            content: Feng.ctxPath + '/storagePutSingle/toPositionStock?positionAreaId='+$("#areaId").val()+'&warehouseId='+$("#warehouseId").val(),
            end: function () {
                $("#positionNumber").val(StoragePutSingleInfoDlg.data.positionNumber);//库位编号
            }
        });
    });
    
    //点击选择商品名称
    $('#choiceg').click(function () {
    	var customerId = $("#customerId").val();
    	if (customerId==null||customerId=='') {
    		layer.msg('Please Select Customer'); 
		} else {
	        layer.open({
	            type: 2,
	            title: '选择商品',
	            area: ['900px', '500px'],
	            content: Feng.ctxPath + '/orderPutDetailed/toChoiceGoods?goodsCustomerId=' + customerId,
	            end: function () {
	                $("#goodsName").val(OrderPutDetailedInfoDlg.data.detailedGoodsname);//商品名称
	                $("#goodsNumber").val(OrderPutDetailedInfoDlg.data.goodsNumber);//商品编号
	                $("#goodsNo").val(OrderPutDetailedInfoDlg.data.goodsNo);
	            }
	        });
		}
    });
    
    //查询库区
    form.on('select(warehouseId)',function(){
    	$("#areaId").empty();
    	$.ajax({
    		url:Feng.ctxPath + '/basicArea/Querylist',
    	    dataType:"json",   //返回格式为json
    	    async: false,
    	    data:{"areaWarehouseId":$("#warehouseId").val()},    //参数值
    	    type:"post",   //请求方式
    	    success:function(data){
    	        if(data.length>0){
    	        	var str = "<option value=''>选择库区</option>";
    	        	$.each(data,function(key,value){
    	        		str += "<option value='"+value.id+"'>"+value.areaName+"</option>";
    	        	});
    	        	$("#areaId").html(str);
    	        }
    	    }
    	});
        form.render('select');
    });

    // 工具条点击事件
    table.on('tool(' + StorageStock.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'toedit') {
            layer.open({
                type: 2,
                title: '修改',
                area: ['900px', '500px'],
                anim: 5,
                content: Feng.ctxPath + '/storageStock/edit?id=' + data.id,
	            end: function () {
                	table.reload(StorageStock.tableId);
	            }
            });
        } else if (layEvent === 'todongjie'){
            layer.open({
                type: 2,
                title: '冻结',
                area: ['900px', '500px'],
                anim: 5,
                content: Feng.ctxPath + '/storageStock/dongjie?id=' + data.id,
	            end: function () {
                	table.reload(StorageStock.tableId);
	            }
            });
		} else if (layEvent === 'tojiedong'){
            layer.open({
                type: 2,
                title: '解冻',
                area: ['900px', '500px'],
                anim: 5,
                content: Feng.ctxPath + '/storageStock/jiedong?id=' + data.id,
	            end: function () {
                	table.reload(StorageStock.tableId);
	            }
            });
		} else if (layEvent === 'tobaofei'){
            layer.open({
                type: 2,
                title: '报废',
                area: ['900px', '500px'],
                anim: 5,
                content: Feng.ctxPath + '/storageStock/baofei?id=' + data.id,
	            end: function () {
                	table.reload(StorageStock.tableId);
	            }
            });
			
		}
    });
    
    // 单击行事件
    table.on('row(' + StorageStock.tableId + ')', function (obj) {
		$('div[lay-id="' + StorageStock.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + StorageStock.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6;" });//改变当前tr颜色
    });

    /**
     * 初始化表格的列
     */
    StorageStockManagement.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'ID'},
            {field: 'panType', sort: true, title: 'Action Type',
            	templet: function(d){
            		var str = "";
            		if (d.panType == '修改') {
                    	str += '<span style="font-weight: 600;">Modify</span>'
					} else if (d.panType == '冻结'){
                    	str += '<span style="color:#e84d74;font-weight: 600;">Freeze</span>'
					} else if (d.panType == '报废'){
                    	str += '<span style="color:#e84d74;font-weight: 600;">Scrap</span>'
					} else if (d.panType == '解冻'){
                    	str += '<span style="color:#00c300;font-weight: 600;">Unfreeze</span>'
					} 
            		return str
            	}
		},
            {field: 'warehouseName', sort: true, title: 'Warehouse', style:'font-weight: 600;'},
            {field: 'areaName', sort: true, title: 'Area', style:'font-weight: 600;'},
            {field: 'positionNumber', sort: true, title: 'Location', style:'font-weight: 600;'},
            {field: 'trayNumber', sort: true, title: 'Tray', style:'font-weight: 600;'},
            {field: 'goodsName', sort: true, title: 'Product', style:'font-weight: 600;'},
            {field: 'goodsNumber', sort: true, title: 'Product No', style:'font-weight: 600;'},
            {field: 'goodsNo', sort: true, title: 'SKU', style:'font-weight: 600;'},
            {field: 'goodsUnit', title: 'Unit', style:'font-weight: 600;'},
            {field: 'goodsPacking', title: 'Packing', style:'font-weight: 600;'},
            {field: 'goodsSpecs', title: 'Specs', style:'font-weight: 600;', totalRowText: 'Total:'},
            {field: 'num', sort: true, title: 'Inventory Qty', style:'font-weight: 600;', totalRow: true},
            {field: 'volume', sort: true, title: 'Inventory Volume', style:'font-weight: 600;', totalRow: true},
            {field: 'cnum', sort: true, title: 'Processed Qty', style:'font-weight: 600;', totalRow: true},
            {field: 'cVolume', sort: true, title: 'Processed Volume', style:'font-weight: 600;', totalRow: true},

            {field: 'producetime', sort: true, title: 'Production Date', style:'font-weight: 600;'},
            {field: 'nproducetime', sort: true, title: 'New Production Date', style:'font-weight: 600;'},
            {field: 'goodsbatchs', sort: true, title: 'Batch', style:'font-weight: 600;'},
            {field: 'ngoodsbatchs', sort: true, title: 'New Batch', style:'font-weight: 600;'},
            {field: 'putTime', sort: true, title: 'Receipt Batch', style:'font-weight: 600;'},
            {field: 'nputTime', sort: true, title: 'New Receipt Batch', style:'font-weight: 600;'},

            {field: 'createTime', sort: true, title: 'Process Time', style:'font-weight: 600;'},
            {field: 'createUserName', title: 'Operator', style:'font-weight: 600;'},
            {field: 'nodes', sort: true, title: 'Description', style:'font-weight: 600;'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    StorageStockManagement.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition2").val();
        table.reload(StorageStockManagement.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 导出excel按钮
     */
    StorageStockManagement.exportExcel = function () {
        var checkRows = table.checkStatus(StorageStockManagement.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("Please select data to export");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + StorageStockManagement.tableId,
        url: Feng.ctxPath + '/storageStockManagement/list',
        page: true,
        limit: 40,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        totalRow: true ,//开启合计行
        cols: StorageStockManagement.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch2').click(function () {
        StorageStockManagement.search();
    });

    // 导出excel
    $('#btnExp2').click(function () {
        StorageStockManagement.exportExcel();
    });
    
    // 单击行事件
    table.on('row(' + StorageStockManagement.tableId + ')', function (obj) {
		$('div[lay-id="' + StorageStockManagement.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + StorageStockManagement.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6;" });//改变当前tr颜色
    });

	

});
